package com.henghai.zhenxuan.ui.page.login;

import android.content.Intent;
import android.os.CountDownTimer;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.config.AppConfig;
import com.henghai.zhenxuan.domain.request.RegisterRequester;
import com.henghai.zhenxuan.domain.request.SendSmsRequester;
import com.henghai.zhenxuan.utils.HiRes;
import com.henghai.zhenxuan.utils.MainHandler;
import com.blankj.utilcode.util.ToastUtils;
import com.henghai.zhenxuan.R;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseActivity;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;

public class RegisterActivity extends BaseActivity {
    private RegisterStates mStates;
    private SendSmsRequester mSendSmsRequester;
    private CountDownTimer countDownTimer;
    private RegisterRequester mRegisterRequester;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(RegisterStates.class);
        mSendSmsRequester = getActivityScopeViewModel(SendSmsRequester.class);
        mRegisterRequester = getActivityScopeViewModel(RegisterRequester.class);
        mRegisterRequester.getTokenResult().observe(this, result -> {
            if (!result.getResponseStatus().isSuccess()) {
                TipDialog.show(result.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            TipDialog.show("注册成功", WaitDialog.TYPE.SUCCESS);
            MainHandler.INSTANCE.postDelay(2000, () -> {
                AppConfig.INSTANCE.getMConfigs().token().set("");
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                RegisterActivity.this.finish();
            });
        });
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(com.henghai.zhenxuan.R.layout.activity_register, BR.vm, mStates).addBindingParam(BR.click, new ClickProxy());
    }

    private void startCountDown() {
        if (countDownTimer != null) {
            countDownTimer.start();
        }
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mStates.codeText.set(millisUntilFinished / 1000 + "");
            }

            @Override
            public void onFinish() {
                mStates.codeText.set("重新发送");
            }
        };
        countDownTimer.start();
    }

    public class ClickProxy {
        public void register() {
            if (!mStates.privacyAgree.get()) {
                com.kunminx.architecture.utils.ToastUtils.showLongToast("请您阅读并同意下方协议");
                return;
            }
            if (mStates.invitationCode.get().isEmpty()) {
                ToastUtils.showShort("请输入邀请码");
                return;
            }
            if (mStates.userPhone.get().isEmpty()) {
                ToastUtils.showShort("请输入手机号");
                return;
            }
            if (mStates.authCode.get().isEmpty()) {
                ToastUtils.showShort("请输入验证码");
                return;
            }
            if (mStates.userPwd.get().isEmpty()) {
                ToastUtils.showShort("请输入密码");
                return;
            }
            mRegisterRequester.request(RegisterActivity.this, mStates.userPhone.get(), mStates.userPwd.get(), mStates.invitationCode.get(), mStates.authCode.get());
        }

        public void send() {
            if (mStates.userPhone.get().isEmpty()) {
                ToastUtils.showShort("请输入手机号");
                return;
            }
            if (mStates.codeText.get().equals("重新发送") || mStates.codeText.get().equals("发送验证码")) {
                mSendSmsRequester.request(RegisterActivity.this, mStates.userPhone.get());
                startCountDown();
            }
        }

        public void pwdBrowse() {
            mStates.pwdBrowse.set(!mStates.pwdBrowse.get());
            if (mStates.pwdBrowse.get()) {
                mStates.pwdCheck.set(R.mipmap.browse_icon_n);
            } else {
                mStates.pwdCheck.set(R.mipmap.browse_icon_y);
            }
        }
    }

    public static class RegisterStates extends StateHolder {

        public final State<Boolean> pwdBrowse = new State<>(false);
        public final State<String> codeText = new State<>("发送验证码");
        public final State<String> userPhone = new State<>("");
        public final State<Integer> pwdCheck = new State<>(R.mipmap.browse_icon_y);
        public final State<String> invitationCode = new State<>("HPYOUTAO1");
        public final State<String> authCode = new State<>("");
        public final State<String> userPwd = new State<>("");
        public final State<Boolean> privacyAgree = new State<>(false);
        public final State<String> privacyContent = new State<>(HiRes.INSTANCE.getString(R.string.privacy_agree));
    }
}
